from qiniu import Auth, put_file, etag, put_data
import qiniu.config

# 需要填写你的 Access Key 和 Secret Key
access_key = 'XXDk--8ZsKS_ioG7o7zuT_FiX2FQ6N8ptML-CsUo'
secret_key = 'GLLl6GQXa6U7Ou2-BiPOo2ned2liJ5IGRC9pQ5Br'


def image_upload(image_data):

    # 构建鉴权对象
    q = Auth(access_key, secret_key)

    # 要上传的空间
    bucket_name = 'flask-user1'

    # 上传后保存的文件名
    key = None

    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    # 二进制传输文件
    ret, info = put_data(token, key, image_data)
    if info.status_code == 200:
        return ret.get("key")
    else:
        return None

    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)


if __name__ == '__main__':
    with open('./成绩单.png', "rb") as f:
        print(image_upload(f.read()))
